Ext.define('HIT.controller.SetMain', {
    extend: 'Ext.app.Controller',
    
    models : [ 'Main' ],
	stores : [ 'Main'] ,
    views  : ['SetMainGrid','SetMainAddPanel','SetMainCopyPanel','SetMainEditPanel'],
    onButtonClick: function(button, e, options) {
    	var p;     
        if(button.id=='set-main-add'){
        	p = Share.createPanelByAlias('widget.setmainaddpanel');
        	Share.showPanelAsWindow(p);
        }
        else if(button.id=='set-main-edit'){
        	 var grid=Ext.getCmp('tab-set-main');           
             if (grid == undefined) {
     			return;
     		}
             var selModel = grid.getSelectionModel();
        	  if (selModel.hasSelection()) {
                  if (selModel.getCount() != 1) {
                	  Share.ShowMessage("I0009","编辑");
                      return;
                  }
                  var model = selModel.getSelection()[0];
                  if (model != undefined) {
                  	p = Share.createPanelByAlias('widget.setmaineditpanel');                              
                      Ext.getCmp('group_name_edit').setValue(model.get('group_name'));
                      Ext.getCmp('group_desc_edit').setValue(model.get('group_desc'));
                      Ext.getCmp('group_id_edit').setValue(model.get('group_id'));                                        
                      Share.showPanelAsWindow(p);
                  }
              }else{
            	Share.ShowMessage("I0011","需要编辑的行");
  				return;
              }
        }
        else if(button.id=='set-main-copy'){
        	 var grid=Ext.getCmp('tab-set-main');           
             if (grid == undefined) {
     			return;
     		}
             var selModel = grid.getSelectionModel();
      	  if (selModel.hasSelection()) {
                if (selModel.getCount() != 1) {
                	Share.ShowMessage("I0009","复制");
                    return;
                }
                var model = selModel.getSelection()[0];
                if (model != undefined) {
                	p = Share.createPanelByAlias('widget.setmaincopypanel');                
                	Ext.getCmp('group_name_copy').setValue(model.get('group_name')+"_copy");
                	Ext.getCmp('group_desc_copy').setValue(model.get('group_desc'));
                	Ext.getCmp('group_id_copy').setValue(model.get('group_id'));           
                    Share.showPanelAsWindow(p);
                }
            }else{
            	Share.ShowMessage("I0011","需要复制的行");
				return;
            }
      }
        else if(button.id=='set-main-delete'){
        	 var grid=Ext.getCmp('tab-set-main');           
             if (grid == undefined) {
     			return;
     		}
             var selModel = grid.getSelectionModel();
             var row=selModel.getSelection();
             if (selModel.hasSelection()) {
            	 if (selModel.getCount() != 1) {
            		 Share.ShowMessage("I0009","删除");
                     return;
                 }
        		var model = selModel.getSelection()[0];	
        		var groupId = model.get('group_id');
        		if(model !=undefined){
        			Ext.MessageBox.confirm(Share.T('提示'),Share.T('确定删除吗？'),function(btn){
	                if(btn == 'yes'){ 	
						Share.AjaxRequest({
							url : ctx + '/deleteMain',
							params : {
								groupId : groupId
							},
							callback : function(json) {
								if (json.resultcode == '0') {					
									grid.getStore().reload();
									var toolbar=Ext.getCmp('setmaintoolbar');
									var fromRecord=toolbar.getPageData().fromRecord;
									var toRecord=toolbar.getPageData().toRecord;
									if(toRecord-fromRecord+1==row.length){
										toolbar.movePrevious();
									}
									Share.ShowMessage("I0006","删除");
								} else if(json.resultcode == '2'){
									Share.ShowMessage("I0037");
								}  else if(json.resultcode == '3'){
									Share.ShowMessage("E0018");
								}
								else {
									Share.ShowMessage("I0007","删除");
								}
							},
							falseFun : function(json) {
								Share.ShowMessage("I0007","删除");
							}
						});
        			   }
        		   })
        		}
            }
        	else{
        		Share.ShowMessage("I0011","需要删除的行");
				return;
        	}
        }
        else if(button.id=='set-main-default'){
       	 var grid=Ext.getCmp('tab-set-main');           
         if (grid == undefined) {
 			return;
 		}
    		  var selModel = grid.getSelectionModel();
              if (selModel.hasSelection()) {
        	    if (selModel.getCount() != 1) {
        	     Share.ShowMessage("I0009","设置引用");
                 return;
             }        	
        	  Ext.MessageBox.confirm(Share.T('提示'),Share.T('确定设置该引用？'),function(btn){
               if(btn == 'yes'){                  
    		     var model = selModel.getSelection()[0];	
    		     var groupId = model.get('group_id');
    		     if(model !=undefined){
    		    	 Share.AjaxRequest({
    		    		 url : ctx + '/setQuoteMain',
    		    		 params : {
    		    			 groupId : groupId
    		    		 },
    		    		 callback : function(json) {
    		    			 if (json.resultcode == '0') {					
    		    				 grid.getStore().reload();
    		    				 Share.ShowMessage("I0047");
    		    			 }else {
    		    				 Share.ShowMessage("I0007","设置");
    		    			 }
    		    		 },
    		    		 falseFun : function(json) {
    		    			 Share.ShowMessage("I0007","设置");
    		    		 }
    		    	 });
    		     }
              }
          }); 
        }
    	else{
    		Share.ShowMessage("I0011","需要进行设置的行");
			return;
    	   }	 
//    		  else {
//    		 Ext.Msg.alert(Share.T('提示'), Share.T("当前公司已经有数据，不可以修改主数据套!"));
//        	 return;
//    	   }
      }
    },
    save:function(win){	 
    	 var grid=Ext.getCmp('tab-set-main');           
         if (grid == undefined) {
 			return;
 		}  
        var functionId = Ext.getCmp('functionId').value;
        if (functionId == 0){
    	var  form = Ext.getCmp('win-setmainaddpanel').down('form');
    	var basicForm = form.getForm();
        if (!basicForm.isValid()) {
        	return;
        }
        groupName = Ext.ComponentMgr.get("group_name").getValue();
        groupDesc = Ext.ComponentMgr.get("group_desc").getValue();        
        if(groupName.trim().length < 1){
        	Share.ShowMessage("I0010","套名称");
        	return;
        }
        if(groupDesc.trim().length < 1){
        	Share.ShowMessage("I0010","套描述");
        	return;
        }
        Ext.Ajax.request({
			url : 'addMain',
			method : 'POST',
			scope: this,
			params : {
				    groupName : groupName
		    	   ,groupDesc : groupDesc
				},
			async : false,
			success : function(response) {
				s = response.responseText;
				json = Ext.JSON.decode(s);
	    /* Share.AjaxRequest({
	      	url : ctx + '/addMain',
	    	params : {
	    		groupName : groupName
	    	   ,groupDesc : groupDesc
		   },*/
			if(json.resultcode == '0') {
				win.close();   
				grid.getStore().reload();   
				Share.ShowMessage("I0006","添加");        	
			} else if (json.resultcode == '1'){
				Share.ShowMessage("I0013","套名称");
			} 
			else {				
				Share.ShowMessage("I0007","添加");     			
			}			
		}
	    }); 
	  }
        else if (functionId == 1){
        	var  form = Ext.getCmp('win-setmaineditpanel').down('form');
        	var basicForm = form.getForm();
            if (!basicForm.isValid()) {
            	return;
            }
            var 
            groupName = Ext.ComponentMgr.get("group_name_edit").getValue(),
            groupDesc = Ext.ComponentMgr.get("group_desc_edit").getValue(); 
            groupId = Ext.ComponentMgr.get("group_id_edit").getValue(); 	
            if(groupName.trim().length < 1){
            	Share.ShowMessage("I0010","套名称");
            	return;
            }
            if(groupDesc.trim().length < 1){
            	Share.ShowMessage("I0010","套描述");
            	return;
            }
            Ext.Ajax.request({
    			url : 'editMain',
    			method : 'POST',
    			scope: this,
    			params : {
	    				groupName : groupName
	     	    	   ,groupDesc : groupDesc
	     	    	   ,groupId  : groupId
    				},
    			async : false,
    			success : function(response) {
    				s = response.responseText;
    				json = Ext.JSON.decode(s);
    	     /*Share.AjaxRequest({
    	      	url : ctx + '/editMain',
    	    	params : {
    	    		groupName : groupName
    	    	   ,groupDesc : groupDesc
    	    	    ,groupId  : groupId
    		   },
    	      callback : function(json) {*/
    			if(json.resultcode == '0') {
    				win.close();   
    				grid.getStore().reload();   
    				Share.ShowMessage("I0006","修改");       	          
    			}else if(json.resultcode == '2'){
    				Share.ShowMessage("I0021"); 
    			}
    			else {  			
    				Share.ShowMessage("I0007","修改");              			
    			}			
    		}
    	    }); 
    	  }
    else if(functionId == 2){
    	var  form = Ext.getCmp('win-setmaincopypanel').down('form');
    	var basicForm = form.getForm();
        if (!basicForm.isValid()) {
        	return;
        }
    	var 
        groupName = Ext.ComponentMgr.get("group_name_copy").getValue(),
        groupDesc = Ext.ComponentMgr.get("group_desc_copy").getValue(); 
        groupId = Ext.ComponentMgr.get("group_id_copy").getValue(); 	
        if(groupName.trim().length < 1){
        	Share.ShowMessage("I0010","套名称");
        	return;
        }
        if(groupDesc.trim().length < 1){
        	Share.ShowMessage("I0010","套描述");
        	return;
        }
         Ext.Ajax.request({
			url : 'copyMain',
			method : 'POST',
			scope: this,
			params : {
					groupName : groupName
	 	    	   ,groupDesc : groupDesc
	 	    	   ,groupId  : groupId
				},
			async : false,
			success : function(response) {
				s = response.responseText;
				json = Ext.JSON.decode(s);
    		/*Share.AjaxRequest({
    	      	url : ctx + '/copyMain',
    	    	params : {
    	    		groupName : groupName
    	    	   ,groupDesc : groupDesc
    	    	    ,groupId  : groupId
    		   },
    	      callback : function(json) {*/
    			if(json.resultcode == '0') {
    				win.close();   
    				grid.getStore().reload(); 
    				Share.ShowMessage("I0006","复制");          		
    			} else if (json.resultcode == '1'){
    				Share.ShowMessage("I0013","套名称");
    			} else {   				
    				Share.ShowMessage("I0007","复制");            				
    			}    			
    		}
    	});
    	}
    },
    init: function(application) {        
        this.control({
            "#headpanel button": {
                click: this.onButtonClick
           }
        });
    }
});